import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { showDialog } from "vant";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      meta: {
        title: "首页",
      },
      component: HomeView,
    },
    {
      path: "/channel",
      name: "channel",
      meta: {
        title: "分类",
        flage: true,
      },
      component: () => import("../views/ChannelView.vue"),
    },
    {
      path: "/mine",
      name: "mine",
      meta: {
        title: "我的",
      },
      component: () => import("../views/MineView.vue"),
    },
    {
      path: "/subject",
      name: "subject",
      meta: {
        title: "专题",
      },
      component: () => import("../views/SubjectView.vue"),
    },
    {
      path: "/classification",
      name: "classification",
      meta: {
        title: "分类",
      },
      component: () => import("../views/ClassificationView.vue"),
    },
    {
      path: "/cart",
      name: "cart",
      meta: {
        title: "购物车",
      },
      component: () => import("../views/CartView.vue"),
    },
    {
      path: "/search",
      name: "search",
      meta: {
        title: "搜索",
        flage: true,
      },
      component: () => import("../views/SearchView.vue"),
    },
    {
      path: "/detail",
      name: "detail",
      meta: {
        title: "详情",
        flage: true,
      },
      component: () => import("../views/DetailsView.vue"),
    },
  ],
});
router.beforeEach((to, from) => {
  document.title = to.meta.title;
  if (to.fullPath === "/cart") {
    if (localStorage.getItem("token")) {
      return true;
    } else {
      showDialog({
        message: "请先登录",
        theme: "round-button",
        width: "250px",
      }).then(() => {
        router.push("/mine");
      });
      return false;
    }
  } else {
    return true;
  }
});

export default router;
